package it.secse.idmadaption.engine;



import java.io.FileNotFoundException;
import java.util.Calendar;
import java.util.TimeZone;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;


import it.secse.google.wrapper.CheckScheduleDocument;
import it.secse.google.wrapper.CheckScheduleResponseDocument;
import it.secse.google.wrapper.RequestMeetingDocument;
import it.secse.idmadaption.credential.CredentialData;
import it.secse.idmadaption.credential.DFWrapper;
import it.secse.idmadaption.credential.UsrPassCredentialData;
import it.secse.idmadaption.credential.UsrPassCredentialData;
import it.secse.idmadaption.exception.PluginRuleNotFoundException;
import it.secse.idmadaption.exception.UnsopportedCredentialTypeException;
import it.secse.idmadaption.plugin.AbstractPlugin;
import it.secse.idmadaption.plugin.PluginFactory;
import it.secse.idmadaption.policy.PolicyDiscover;
import it.secse.idmadaption.policy.PolicyWrapper;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.commons.httpclient.UsernamePasswordCredentials;


public class IDMAdapter implements IDMAdapterIF {

	/**
	 * @param args
	 */
	
	private static CredentialData credential = null;
	
	public static void main(String[] args) {
		IDMAdapter adapt = new IDMAdapter();
		try {
			CheckScheduleDocument request = CheckScheduleDocument.Factory.newInstance();
			CheckScheduleDocument.CheckSchedule  param = CheckScheduleDocument.CheckSchedule.Factory.newInstance();
			param.setStrMailbox("lucabitta@gmail.com");

			// SISTEMARE ORE +2
			Calendar arg0 = Calendar.getInstance();	
			arg0.add(Calendar.HOUR_OF_DAY, 2);
			TimeZone zone = arg0.getTimeZone();
			param.setStart(arg0);
			System.out.println(arg0.getTimeZone().toString());
			Calendar arg1 = Calendar.getInstance();
			arg1.add(Calendar.HOUR_OF_DAY, 4);
			param.setEnd(arg1);			
			System.out.println(arg1.getTime().toLocaleString());
			request.setCheckSchedule(param);
		
			

//			adapt.receive("checkSchedule", "https://10.10.2.12:8443/axis2/services/ut-over-https", request, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");

//			adapt.receive("checkSchedule", "https://127.0.0.1:8443/axis2/services/ut-over-https", request, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
//pc fabioo
//			adapt.receive("checkSchedule", "https://10.10.2.12:8443/axis2/services/ut-over-https", request, "gianluca.ripa@gmail.com", "http://" + "10.10.2.12" + ":8015/clientcallback");
//			CheckScheduleResponseDocument response = (CheckScheduleResponseDocument) adapt.receive("test", "http://127.0.0.1:8080/test", request, "lucabitta@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
			
			CheckScheduleResponseDocument response = (CheckScheduleResponseDocument) adapt.receive("checkSchedule", "https://127.0.0.1:8443/axis2/services/GoogleWrapper", request, "lucabitta@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
//			CheckScheduleResponseDocument response = (CheckScheduleResponseDocument) adapt.receive("checkSchedule", "https://10.10.2.12:8443/axis2/services/ut-over-https", request, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
			
			boolean bolResponse = response.getCheckScheduleResponse().getReturn();
			
			if (bolResponse == false) {
				
				RequestMeetingDocument document = RequestMeetingDocument.Factory.newInstance();
				RequestMeetingDocument.RequestMeeting meeting = RequestMeetingDocument.RequestMeeting.Factory.newInstance();
				meeting.setMailbox("lucabitta@gmail.com");
				meeting.setRequestSubject("Travel");
				meeting.setDescription("Travel on Rome");
				meeting.setLocation("On the Road");
				Calendar start = Calendar.getInstance();
				start.add(Calendar.HOUR_OF_DAY, 2);
				meeting.setStart(start);
				Calendar end = Calendar.getInstance();
				end.add(Calendar.HOUR_OF_DAY, 4);
				meeting.setEnd(end);
				document.setRequestMeeting(meeting);
				
//				adapt.receive("requestMeeting", "https://127.0.0.1:8443/axis2/services/ut-over-https", document, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
				
			    adapt.receive("requestMeeting", "https://127.0.0.1:8443/axis2/services/GoogleWrapper", document, "lucabitta@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
				
				
				
			}

	        System.out.println(response.getCheckScheduleResponse().getReturn());
	    		
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	
	public Object receive(String operationName, String realEndPoint,
			Object clientRequest, String delegator, String callback)
			throws AxisFault {
		
		
		
			try {
				PolicyWrapper policy = PolicyDiscover.getPolicy(operationName);
				System.out.println(policy.getPolicyType());
				credential = DFWrapper.getCredential(realEndPoint, delegator, callback);
				AbstractPlugin plug = PluginFactory.getPlugin(policy.getPolicyType());
				Object response = plug.manage(operationName, realEndPoint, clientRequest, credential, policy);
				return response;
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsopportedCredentialTypeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PluginRuleNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		
		return null;

	}
	
	public static CredentialData getCredential() {
		return credential;
	}

}
